<template>
  <div class="app-container">
    <el-form :inline="true" :model="formInline" class="demo-form-inline">
      <el-form-item label="所属公司">
        <el-select v-model="formInline.gongsiSel" @change="initData" placeholder="所属公司">
          <el-option v-for="val in gongsiList" :key="val.id" :label="val.name" :value="val.id"></el-option>
        </el-select>
      </el-form-item>
      <!-- <el-form-item label="设备名称">
        <el-input v-model="formInline.orderNumId" placeholder="设备名称"></el-input>
      </el-form-item>
      <el-form-item label="设备序列号">
        <el-select v-model="formInline.orderState" placeholder="设备序列号">
          <el-option v-for="val in orderList" :key="val.value" :label="val.label" :value="val.value"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="设备地址">
        <el-input v-model="formInline.orderNumId" placeholder="设备地址"></el-input>
      </el-form-item>
      <el-form-item label="设备组选择">
        <el-select v-model="formInline.orderState" placeholder="设备组选择">
          <el-option v-for="val in orderList" :key="val.value" :label="val.label" :value="val.value"></el-option>
        </el-select>
      </el-form-item> -->
      <!-- <el-form-item>
        <el-button type="primary" @click="check">查询</el-button>
      </el-form-item> -->
    </el-form>
    <el-button type="primary" icon="el-icon-download" @click="creadOrder">
      生成货单
    </el-button>

    <el-table :data="dataList" @selection-change="handleSelectionChange" style="width: 100%;margin-top:30px;" border>
      <el-table-column
        type="selection">
      </el-table-column>
      <el-table-column align="center" prop="name" label="设备名称">
        <template slot-scope="scope">
          {{ scope.row.device.name }}
        </template>
      </el-table-column>
      <el-table-column align="center" label="设备序列号">
        <template slot-scope="scope">
          {{ scope.row.device.deviceSn }}
        </template>
      </el-table-column>
      <el-table-column align="center" label="设备虚拟点位">
        <template slot-scope="scope">
          {{ scope.row.device.location }}
        </template>
      </el-table-column>
      <el-table-column align="center" label="设备分组">
        <template slot-scope="scope">
          {{ scope.row.device.deviceGroupName }}
        </template>
      </el-table-column>
      <el-table-column align="center" label="库存更新时间">
        <template slot-scope="scope">
          {{ timeChange(scope.row.device.created) }}
        </template>
      </el-table-column>
      <el-table-column align="center" prop="name" label="设备库存状态">
        <template slot-scope="scope">
          <el-progress v-if="scope.row.inStoreCount != 0" :percentage="(Number(scope.row.inStoreCount)*100/Number(scope.row.maxCount)).toFixed(0)" :color="customColor"></el-progress>          
          <el-progress v-else :percentage="0" :color="customColor"></el-progress>
        </template>
      </el-table-column>
      <el-table-column align="center" fixed="right" label="操作">
        <template slot-scope="scope">
          <el-button type="primary" size="small" @click="handleEdit(scope)">
            详情
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <el-dialog :visible.sync="dialogVisible" title="商品详情">
      <el-table :data="deviceDetail" style="width: 100%;margin-top:30px;" border>
        <el-table-column align="center" prop="name" label="排，列">
          <template slot-scope="scope">
            {{ scope.row.skuLoc }}
          </template>
        </el-table-column>
        <el-table-column align="center" label="商品名称">
          <template slot-scope="scope">
            {{ scope.row.skuName }}
          </template>
        </el-table-column>
        <el-table-column align="center" label="商品条码">
          <template slot-scope="scope">
            {{ scope.row.skuBarcode }}
          </template>
        </el-table-column>
        <el-table-column align="center" label="商品图片">
          <template slot-scope="scope">
            <img style="width: 50px;" :src="scope.row.skuImg" alt="">
          </template>
        </el-table-column>
        <el-table-column align="center" label="商品库存">
          <template slot-scope="scope">
            {{ scope.row.currentCount }}
          </template>
        </el-table-column>
        <el-table-column align="center" prop="name" label="商品总量">
          <template slot-scope="scope">
            {{ scope.row.maxCount }}
          </template>
        </el-table-column>
      </el-table>
    </el-dialog>
    <el-dialog :visible.sync="dialogFillBuckle" title="商品补扣">
      <el-form :model="role" :rules="rules" label-width="160px" label-position="left">
        <el-form-item label="订单号">
          <el-input v-model="role.orderNumId" placeholder="订单号"></el-input>
        </el-form-item>
      </el-form>
      <el-form :model="role" :rules="rules" label-width="160px" label-position="left">
        <el-form-item label="扣款金额">
          <el-input v-model="role.orderNumId" type="number" placeholder="扣款金额"></el-input>
        </el-form-item>
      </el-form>
      <div style="text-align:right;">
        <el-button type="danger" @click="dialogFillBuckle=false">
          取消
        </el-button>
        <el-button type="primary" @click="confirmRole">
          确定
        </el-button>
      </div>
    </el-dialog>
    <el-dialog :visible.sync="dialogRefund" title="商品退款">
      <el-form :model="role" :rules="rules" label-width="160px" label-position="left">
        <el-form-item label="订单号">
          <el-input v-model="role.orderNumId" placeholder="订单号"></el-input>
        </el-form-item>
      </el-form>
      <el-form :model="role" :rules="rules" label-width="160px" label-position="left">
        <el-form-item label="扣款金额">
          <el-input v-model="role.orderNumId" type="number" placeholder="扣款金额"></el-input>
        </el-form-item>
      </el-form>
      <div style="text-align:right;">
        <el-button type="danger" @click="dialogRefund=false">
          取消
        </el-button>
        <el-button type="primary" @click="confirmRole">
          确定
        </el-button>
      </div>
    </el-dialog>
    <Pagination :total="total" :page="page+1" @pagination="pagination" />
  </div>
</template>

<script>
import { deepClone } from '@/utils'
import { getInventory, postInventoryFind, postInventoryUpdate } from '@/api/DeviceInventory'
import {  postTicket } from '@/api/OperationTicket'
import Pagination from '@/components/Pagination/index'

const defaultRole = {
  key: '',
  name: '',
  address: '',
  phone:'',
  state: '1',
  adminName:'',
  routes: []
}

export default {
  data() {
    return {
      role: Object.assign({}, defaultRole),
      routes: [],
      dataList: [],
      dialogVisible: false,
      dialogFillBuckle: false,
      dialogRefund: false,
      dialogType: 'new',
      checkStrictly: false,
      defaultProps: {
        children: 'children',
        label: 'title'
      },
      customColor: '#67c23a',
      rules: {
        name: [
          { required: true, message: '请输入设备名称' },
        ],
        address: [
          { required: false, message: '请输入公司地址' },
        ],
        phone: [
          { required: false, message: '请输入联系方式' },
        ],
        phone: [
          { required: false, message: '请输入联系方式' },
        ],
      },
      orderList: [],
      total: 0,
      page: 0,
      formInline: {
        gongsiSel: ''
      },
      deviceDetail: {},
      multipleSelection:[],
    }
  },
  computed: {
  },
  components: { Pagination },
  created() {
    this.gongsiList = JSON.parse(localStorage.getItem('companyList'));
    this.formInline.gongsiSel = this.gongsiList[0].id;
    this.initData();
  },
  methods: {
    //table选择的商品
    handleSelectionChange(val) {
      console.log(val)
      this.multipleSelection = val;
    },
    //列表
    initData(){
      var data = {
        "pageCount": 10,
        "page": this.page,
        "searchCriteria": {
          "tenantId": this.formInline.gongsiSel
        }
      };
      postInventoryFind(data).then((res)=>{
        console.log(res)
        this.dataList = res.result.inventories;
        this.total = res.result.totalCount;
      })
    },    
    //创建工单
    creadOrder(){
      if(this.multipleSelection.length == 0){
        this.$message({
            type: 'error',
            message: '请先选择设备!'
          })
      }else{
        var arr = [];
        for(let i=0;i<this.multipleSelection.length;i++){
          arr.push(this.multipleSelection[i].device.id)
        }
      }
      var data = { "deviceIds": arr }
      postTicket(data).then((res)=>{
          this.$message({
            type: 'success',
            message: '创建成功!'
          })
      })
    },
    handleDelete({ $index, row }) {
      this.$confirm('确认删除?', '删除', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(async() => {
          this.dataList.splice($index, 1)
          this.$message({
            type: 'success',
            message: 'Delete succed!'
          })
        })
        .catch(err => { console.error(err) })
    },
    //导出
    handleExport(){

    },
    //重置
    handleReset(){
      this.formInline = {
        orderDate: '',
        orderNumId: '',
        orderState: 1
      };
    },
    //过滤查询
    check(){
      this.initData();
    },
    //时间格式转化
    timeChange(UTCDateString) {
      if (!UTCDateString) {
        return '-';
      }
      function formatFunc(str) {
        return str > 9 ? str : '0' + str
      }
      var date2 = new Date(UTCDateString);
      console.log('时间', date2)
      var year = date2.getFullYear();
      var mon = formatFunc(date2.getMonth() + 1);
      var day = formatFunc(date2.getDate());
      var hour = date2.getHours();
      var noon = hour >= 12 ? 'PM' : 'AM'; // 判断是上午还是下午
      hour = hour >= 12 ? hour - 12 : hour; // 12小时制
      hour = formatFunc(hour);
      var min = formatFunc(date2.getMinutes());
      var sec = formatFunc(date2.getSeconds());
      var dateStr = year + '-' + mon + '-' + day + ' ' + hour + ':' + min + ':' + sec;
      return dateStr;
    },
    //分页
    pagination(data) {
      this.page = data.page-1;
      this.initData();
    },
    handleAddRole() {
      this.role = Object.assign({}, defaultRole)
      if (this.$refs.tree) {
        this.$refs.tree.setCheckedNodes([])
      }
      this.dialogType = 'new'
      this.dialogVisible = true
    },
    handleEdit(scope) {
      console.log(scope)
      this.dialogType = 'edit';
      this.dialogVisible = true;
      getInventory(scope.row.device.id).then((res)=>{
        console.log(res)
        this.deviceDetail = res.result.details;
      })
    },
    handleOverlock({ $index, row }) {
      this.$confirm('确认忽略?', '忽略', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(async() => {
          this.dataList.splice($index, 1)
          this.$message({
            type: 'success',
            message: 'Delete succed!'
          })
        })
        .catch(err => { console.error(err) })
    },
    async confirmRole() {
      
      this.dialogVisible = false
    },
  }
}
</script>

<style lang="scss" scoped>
.app-container {
  .roles-table {
    margin-top: 30px;
  }
  .permission-tree {
    margin-bottom: 30px;
  }
}
</style>
